package com.xafc.demo.framework.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2WebMvc
@EnableKnife4j  // 该注解是knife4j提供的增强注解,Ui提供了例如动态参数、参数过滤、接口排序等增强功能,如果你想使用这些增强功能就必须加该注解，否则可以不用加.
public class Knife4jConfig {

    //是否开启swagger，正式环境一般是需要关闭的，可根据springboot的多环境配置进行设置
    @Value(value = "${swagger.enabled}")
    Boolean swaggerEnabled;

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("接口文档")
                        .description("# swagger-bootstrap-ui-demo RESTFUL APIs")
                        .termsOfServiceUrl("http://www.xxx.com/")
                        .contact(new Contact("ContactName", "ContactUrl", "xxx@163.com"))
                        .version("1.0")
                        .build())
                .enable(swaggerEnabled)
                //分组名称
                .groupName("2.X版本")
                .select()
                //这里指定Controller扫描包路径
                // (第一种方式)扫描所有有注解的api，用这种方式更灵活
                .apis( RequestHandlerSelectors.withMethodAnnotation( ApiOperation.class ) )
                // (第二种方式)扫描指定包中的swagger注解
                //.apis(RequestHandlerSelectors.basePackage("com.xafc.demo.sys.controller"))
                // (第三种方式)扫描所有
                //.apis(RequestHandlerSelectors.any())
                //.paths( PathSelectors.any() )
                // (第四种方式)只显示api路径下的页面
                //.paths(Predicates.and(PathSelectors.regex("/api/.*")))
                .paths(PathSelectors.any())
                .build();
    }
}